package com.example.pceb.controller;

import com.example.pceb.entity.CooperationApplication;
import com.example.pceb.entity.SchoolEnterpriseCooperation;
import com.example.pceb.service.CooperationApplicationService;
import com.example.pceb.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/cooperation-application")
//@CrossOrigin(origins = "*")
public class CooperationApplicationController {

    private static final Logger logger = LoggerFactory.getLogger(CooperationApplicationController.class);

    @Autowired
    private CooperationApplicationService service;

    @PostMapping("/submit")
    public String submitApplication(@RequestBody CooperationApplication application) {
        try {
            logger.info("收到申请提交请求，申请者: {}, 合作项目ID: {}, 发布者用户名: {}", 
                application.getApplicantName(), application.getCooperationId(), application.getPublisherUsername());
            
            service.submitApplication(application);
            
            logger.info("申请提交成功，申请者: {}, 发布者用户名: {}", 
                application.getApplicantName(), application.getPublisherUsername());
            return "success";
        } catch (IllegalArgumentException e) {
            logger.warn("申请提交失败，参数错误: {}", e.getMessage());
            return e.getMessage();
        } catch (Exception e) {
            logger.error("申请提交失败，系统错误", e);
            return "系统错误，请稍后重试";
        }
    }

    @GetMapping("/applications")
    public List<CooperationApplication> getApplicationsByPublisher(
            @RequestParam String publisher_username) {
        List<CooperationApplication> list = service.getApplicationsByPublisher(publisher_username);
        System.out.println("查到的申请数量: " + list.size());
        return list;
    }

    @GetMapping("/applications/by-type")
    public List<CooperationApplication> getApplicationsByPublisherType(
            @RequestParam String publisher_type) {
        List<CooperationApplication> list = service.getApplicationsByPublisherType(publisher_type);
        System.out.println("查到的申请数量: " + list.size());
        return list;
    }

    @PostMapping("/review")
    public Result<String> reviewApplication(@RequestBody CooperationApplication application) {
        try {
            logger.info("收到审核请求，ID: {}, 状态: {}, 备注: {}", 
                application.getId(), application.getApplicationStatus(), application.getReviewRemarks());
            service.reviewApplication(application);
            logger.info("审核完成，ID: {}", application.getId());
            return Result.success("审核成功");
        } catch (Exception e) {
            logger.error("审核失败", e);
            return Result.error("审核失败：" + e.getMessage());
        }
    }
    
    @PostMapping("/cancel-application")
    public Result<String> cancelApplication(@RequestBody CooperationApplication application) {
        try {
            logger.info("收到取消申请请求，ID: {}, 申请者: {}", 
                application.getId(), application.getApplicantName());
            service.cancelApplication(application.getId(), application.getApplicantName());
            logger.info("申请取消完成，ID: {}", application.getId());
            return Result.success("申请已取消");
        } catch (Exception e) {
            logger.error("取消申请失败", e);
            return Result.error("取消申请失败：" + e.getMessage());
        }
    }

    @GetMapping("/my-applications")
    public Result<List<Map<String, Object>>> getMyApplications(@RequestParam String username) {
        logger.info("收到我的申请请求，用户名: {}", username);
        List<Map<String, Object>> result = service.getMyApplicationsWithProject(username);
        logger.info("查询到申请数量: {}", result != null ? result.size() : 0);
        return Result.success(result);
    }

    @GetMapping("/project-detail")
    public Result<SchoolEnterpriseCooperation> getProjectDetail(@RequestParam Long id) {
        logger.info("收到项目详情请求，项目ID: {}", id);
        SchoolEnterpriseCooperation project = service.selectProjectById(id);
        logger.info("查询到项目: {}", project != null ? project.getProjectName() : "null");
        return Result.success(project);
    }
}